<?php
namespace app\admin\model;
use app\common\controller\Files;
use app\common\controller\Excel;
use think\Db;

class ImportModel
{
	/**
	* 导入
	*/
	public static function import($dbname,$field)
	{
		$path = ROOT_PATH . 'public' . DS . 'upload' . DS . 'excel' . DS . 'cache' . DS;
		$upload = Files::upload($path,'excel');
		if($upload['code'] == 10001)
		{
			$sheetData = Excel::Import($upload['msg']);
			 @unlink($upload['msg']);
			 //去掉第一行
			 unset($sheetData[0]);
			 if(empty($sheetData))
			 {
				 return back(10004,'表格内容为空');
			 }
			 $data = [];
			 foreach ($sheetData as $key=>$value)
			 {
				if(empty($value[0]) || empty($value[1])){
					continue;
				}
				foreach($field as $i=>$name)
				{
					$data[$key-1][$name] = $value[$i];
				}
			 }
			if(Db::name($dbname)->insertAll($data))
			{
				return back(10001,'添加成功');
			}
			return back(10005,'添加失败');
		}
		return $upload;
	}
}
